package com.JobWebGem.system.controller;

import com.JobWebGem.system.annotation.AuthCheck;
import com.JobWebGem.system.common.BaseResponse;
import com.JobWebGem.system.common.DeleteRequest;
import com.JobWebGem.system.common.ErrorCode;
import com.JobWebGem.system.common.ResultUtils;
import com.JobWebGem.system.constant.UserConstant;
import com.JobWebGem.system.exception.BusinessException;
import com.JobWebGem.system.exception.ThrowUtils;
import com.JobWebGem.system.model.dto.type.TypeAddRequest;
import com.JobWebGem.system.model.entity.Type;
import com.JobWebGem.system.service.TypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 工作类别接口
 *
 * @author renli
 */
@RestController
@RequestMapping("/type")
@Slf4j
@Api(tags = "工作类别接口")
public class TypeController {

    @Resource
    private TypeService typeService;

    // region 增删改查

    /**
     * 创建工作类别(仅管理员)
     *
     * @param typeAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("创建工作类别(仅管理员)")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Integer> addType(@RequestBody TypeAddRequest typeAddRequest, HttpServletRequest request) {
        if (typeAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
        }
        Type type = new Type();
        BeanUtils.copyProperties(typeAddRequest, type);
        boolean result = typeService.save(type);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "创建失败");
        return ResultUtils.success(type.getId());
    }

    /**
     * 删除工作类别
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    @ApiOperation("删除工作类别")
    public BaseResponse<Boolean> deleteType(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean result = typeService.removeById(deleteRequest.getId());
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR, "删除失败");
        return ResultUtils.success(result);
    }

    /**
     * 分页获取所有工作类别
     *
     * @return
     */
    @PostMapping("/list/page")
    @ApiOperation("分页获取所有工作类别")
    public BaseResponse<Page<Type>> listUserByPage() {
        QueryWrapper<Type> queryWrapper = new QueryWrapper<>();

        List<Type> result = typeService.list(queryWrapper);
        ThrowUtils.throwIf(result == null, ErrorCode.NOT_FOUND_ERROR, "没有数据");
        Page<Type> typePage = new Page<>(1, 10);
        typePage.setRecords(result);
        typePage.setTotal(result.size());
        return ResultUtils.success(typePage);
    }

    // endregion

}
